package com.study.indusry;

import java.io.*;

/**
 * 稀数组算法
 * 将二维数组转换成稀数组存入到txt文件中
 * 再将txt文件中的稀数组读取出来转换成二维数组
 */
public class SparseArray {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //构建二维数组
        int[][] ints = new int[11][11];
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints.length; j++) {
                ints[i][j] = 0;
            }
        }
        ints[1][3] = 1;
        ints[2][4] = 2;
        ints[2][5] = 1;
        SparseArray.arrayToSparseArray("D:\\MyPorjecat\\ztest\\DataStructure\\src\\稀数组.txt",ints);
        ints = SparseArray.sparseArrayToArray("D:\\MyPorjecat\\ztest\\DataStructure\\src/稀数组.txt");

        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints.length; j++) {
                System.out.print(ints[i][j]+"\t");
            }
            System.out.println();
        }
    }

    /**
     * 数组转稀数组并存入文件
     * @return
     */
    public static void arrayToSparseArray(String path,int[][] array) throws IOException {
        //循环数组找出有效数据
        int cont = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                if (array[i][j] != 0) {
                    cont++;
                }
            }
        }
        int[][] ints = new int[cont + 1][3];
        //存入行
        ints[0][0] = array.length;
        //存入列
        ints[0][1] = array[0].length;
        //存入个数
        ints[0][2] = cont;
        //计数器
        int counter = 1;
        for (int i = 0; i < array.length; i++){
            for (int j = 0; j < array.length; j++) {
                if(array[i][j] != 0){
                    ints[counter][0] = i;
                    ints[counter][1] = j;
                    ints[counter][2] = array[i][j];
                    counter++;
                }
            }
        }
        //写入文件
        //找到目标文件
        File file = new File(path);
        //建立数据通道
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //建立对象输出流对象
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(ints);
    }

    /**
     * 从文件读取稀数组转数组
     * @return
     */
    public static int[][] sparseArrayToArray(String path) throws IOException, ClassNotFoundException {
        //读取文件
        //找到目标文件
        File file = new File(path);
        //建立数据通道
        FileInputStream fileInputStream = new FileInputStream(file);
        //建立对象输入流对象
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        int[][] ints = (int[][])objectInputStream.readObject();
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints[i].length; j++) {
                System.out.print(ints[i][j]+"\t");
            }
            System.out.println();
        }
        //把稀数组转换成数组
        int[][] arry = new int[ints[0][0]][ints[0][1]];
        for (int i = 1; i <= ints[0][2]; i++){
            arry[ints[i][0]][ints[i][1]] = ints[i][2];
        }
        return arry;
    }

}
